home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 2: Applications / Linux Cubed Series 2 - Applications.iso / math / gle-3.000 / gle-3 / gle / easydev.c < prev    next >
C/C++ Source or Header  |  1995-02-07  |  4KB  |  179 lines

  1. /* Easy to use default routines for simple drivers (line output only) */
  2. /*---------------------------------------------------------------------------*/
  3. #include <math.h>
  4. #include "all.h"
  5. #include "mygraph.h"
  6. #include "mydev.h"
  7. #include "core.h"
  8. #include "justify.h"
  9. extern struct gmodel g;
  10. /*---------------------------------------------------------------------------*/
  11. #if (defined M_PI && !defined PI)
  12. #define PI M_PI
  13. #define pi M_PI
  14. #elif defined PI
  15. #define pi PI
  16. #else
  17. #define PI 3.141592653
  18. #define pi 3.141592653
  19. #endif
  20.  
  21. #define true (!false)
  22. #define false 0
  23. int clipvec(dbl x1,dbl y1,dbl x2,dbl y2,dbl wxmin,dbl wymin,dbl wxmax,dbl wymax);
  24. int xdf_barc(double r,dbl t1,dbl t2,dbl cx,dbl cy);
  25. /*---------------------------------------------------------------------------*/
  26. #define CSTEP (360/6)
  27.  
  28. df_bezier(dbl x1,dbl y1,dbl x2,dbl y2,dbl x3,dbl y3)
  29. {
  30.     double ax,bx,cx,ay,by,cy,dist;
  31.     double xxx,yyy,i,t,nstep,x0,y0;
  32.     g_get_xy(&x0,&y0);
  33.     dist = fabs(x3-x0) + fabs(y3-y0);
  34.     nstep = 10;
  35.     if (dist>3) nstep = 20;
  36.     if (dist<.5) nstep = 5;
  37.     if (dist<.3) nstep = 3;
  38.     if (dist<.1) {
  39.         g_line(x3,y3);
  40.         return;
  41.     }
  42.     cx = (x1-x0)*3;
  43.     bx = (x2-x1)*3-cx;
  44.     ax = x3-x0-cx-bx;
  45.     cy = (y1-y0)*3;
  46.     by = (y2-y1)*3-cy;
  47.     ay = y3-y0-cy-by;
  48.     for (i=0;i<=nstep;i++) {
  49.         t = i/nstep;
  50.         xxx = ax*pow(t,3.0) + bx*t*t + cx*t + x0;
  51.         yyy = ay*pow(t,3.0) + by*t*t + cy*t + y0;
  52.         g_line(xxx,yyy);
  53.     }
  54. }
  55.  
  56. df_arcto(dbl x1,dbl y1,dbl x2,dbl y2,dbl rrr)
  57. {
  58.     double x0,y0,r1,a1,r2,a2,r3,a3,a4,r5,sx,sy,ex,ey;
  59.     double bx1,by1,bx2,by2,dist,neg;
  60.     g_get_xy(&x0,&y0);
  61.     xy_polar(x1-x0,y1-y0,&r1,&a1);
  62.     xy_polar(x2-x1,y2-y1,&r2,&a2);
  63.     neg = 1;
  64.     a4 = (180-a2+a1);
  65.     a3 = a2 + (a4/2);
  66.     if ((a4/2)>90 && (a4/2)<180 ) neg = -1;
  67.     if ((a4/2)<0 && (a4/2)>-90 ) neg = -1;
  68.     r3 = neg*rrr/(tan((pi/180)*a4/2));
  69.     polar_xy(-r3,a1,&sx,&sy); sx += x1; sy += y1;
  70.     polar_xy(r3,a2,&ex,&ey); ex += x1; ey += y1;
  71.     g_line(sx,sy);
  72.     dist = sqrt((ex-sx)*(ex-sx) + (ey-sy)*(ey-sy));
  73.     polar_xy(r1+ dist/2.5-r3,a1,&bx1,&by1); bx1 += x0; by1 += y0;
  74.     polar_xy(-r2+ -dist/2.5+r3,a2,&bx2,&by2); bx2 += x2; by2 += y2;
  75.     g_bezier(bx1,by1,bx2,by2,ex,ey);
  76.     g_line(x2,y2);
  77. }
  78. df_arc(dbl r,dbl t1,dbl t2,dbl cx,dbl cy)
  79. {
  80.     /* circle from t1 to t2, lets use 6 bezier's for a circle */
  81.     double stz;
  82.     int nst,i;
  83.  
  84.     /* if (t2<t1) t2 = t2 + 360 ; a.r. */
  85.     for (;t2<t1;)
  86.           t2 = t2 + 360;
  87.  
  88.     nst = floor((t2-t1)/CSTEP)+1;
  89.     stz = (t2-t1) / nst;
  90.     for (i=1;i<=nst;i++)
  91.         xdf_barc(r,t1+stz*(i-1),t1+stz*i,cx,cy);
  92. }
  93. xdf_barc(double r,dbl t1,dbl t2,dbl cx,dbl cy)
  94. {
  95.     double rx1,ry1,rx2,ry2,d,dx1,dy1,dx2,dy2;
  96.  
  97.     polar_xy(r,t1,&rx1,&ry1);
  98.     polar_xy(r,t2,&rx2,&ry2);
  99.     d = sqrt( (rx2-rx1)*(rx2-rx1) + (ry2-ry1)*(ry2-ry1));
  100.     polar_xy(d/3,t1+90,&dx1,&dy1);
  101.     polar_xy(d/3,t2-90,&dx2,&dy2);
  102.     if (g.inpath) {
  103.         g_line(rx1+cx,ry1+cy);
  104.         g_bezier(rx1+cx+dx1,ry1+cy+dy1
  105.             ,rx2+cx+dx2,ry2+cy+dy2,rx2+cx,ry2+cy);
  106.     } else {
  107.         g_move(rx1+cx,ry1+cy);
  108.         g_bezier(rx1+cx+dx1,ry1+cy+dy1
  109.             ,rx2+cx+dx2,ry2+cy+dy2,rx2+cx,ry2+cy);
  110.         g_move(cx,cy);
  111.     }
  112. }
  113. df_box_stroke(dbl x1, dbl y1, dbl x2, dbl y2)
  114. {
  115.     double ox,oy;
  116.     g_get_xy(&ox,&oy);
  117.     g_move(x1,y1);
  118.     g_line(x2,y1);
  119.     g_line(x2,y2);
  120.     g_line(x1,y2);
  121.     g_closepath();
  122.     g_move(ox,oy);
  123. }
  124.  
  125. df_box_fill(dbl x1, dbl y1, dbl x2, dbl y2)
  126. {
  127.     double dx,dy,cs,x,y;
  128.     int32 fc,oldcolor;
  129.     colortyp  cc;
  130.     g_get_fill(&fc);
  131.     cc.l = fc;
  132.     cs = cc.b[B_R]*3 + cc.b[B_B] + cc.b[B_G]*2;
  133.     if (cs>(6*250)) return; /* white, dont try and fill it in */
  134.     cs = (cs+50)/700;
  135.  
  136.     if (x1>x2) {dx = x1; x1 = x2; x2 = dx; }
  137.     if (y1>y2) {dy = y1; y1 = y2; y2 = dy; }
  138.     dx = x2-x1;
  139.     dy = y2-y1;
  140.     if (dx==0 || dy==0) return;
  141.         /* cs = color intensity  (measure in cm) */
  142.         /* set pen color to fill color now */
  143.     g_get_color(&oldcolor);
  144.     g_get_fill(&fc);
  145.     g_set_color(fc);
  146.     for (y = y1-dx; y<y2; y+=cs) {
  147.         clipvec(x1,y,x2,y+cs-.4,x1,y1,x2,y2);
  148.     }
  149.     g_set_color(oldcolor);
  150. }
  151. clipvec(dbl x1,dbl y1,dbl x2,dbl y2,dbl wxmin,dbl wymin,dbl wxmax,dbl wymax)
  152. {
  153.  
  154.     int invis;
  155.     if (x1>=wxmin && x1<=wxmax && y1>=wymin && y1<=wymax
  156.       && x2 >= wxmin && x2<=wxmax && y2>=wymin && y2<=wymax);
  157.     else {          /* ok one or both are outside our box */
  158.         gclip(&x1,&y1,&x2,&y2,wxmin,wymin,wxmax,wymax,&invis);
  159.         if (invis) return;
  160.     }
  161.     g_move(x1,y1);
  162.     g_line(x2,y2);
  163. }
  164. df_circle_stroke(dbl r)
  165. {
  166.     double cx,cy;
  167.     g_get_xy(&cx,&cy);
  168.     df_arc(r,0.0,360.0,cx,cy);
  169. }
  170. df_circle_fill(dbl r)
  171. {
  172.     double cx,cy;
  173.     g_get_xy(&cx,&cy);
  174.     df_arc(r,0.0,360.0,cx,cy);
  175. }
  176.  
  177.  
  178.  
  179.